<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item label="保护点名称" prop="name">
        <el-input v-model="pram.name" maxlength="100" placeholder="保护点名称" />
      </el-form-item>
<!--      <el-form-item label="类型" prop="type">-->
<!--        <el-select v-model="pram.type" placeholder="类型" clearable style="width: 100%">-->
<!--          <el-option-->
<!--            v-for="(item,index) in categoryData.typeList"-->
<!--            :key="index"-->
<!--            :label="item.name"-->
<!--            :value="item.id"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="分布区域" prop="area">-->
<!--        <el-input v-model="pram.area" maxlength="255" placeholder="分布区域" />-->
<!--      </el-form-item>-->
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="经度" prop="longitude">
            <el-input v-model.number="pram.longitude" type="number" placeholder="经度" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="纬度" prop="latitude">
            <el-input v-model.number="pram.latitude" type="number" placeholder="纬度" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="保护半径" prop="radius">
        <el-input v-model="pram.radius" maxlength="150" placeholder="保护半径" />
      </el-form-item>
      <el-form-item label="所属单位" prop="departmentId">
        <el-cascader v-model="pram.departmentId" :disabled="pram.departmentId ===0" :options="parentOptions" :props="categoryProps" style="width:100%" />
        <!-- allTreeList or parentOptions-->
      </el-form-item>
      <el-form-item label="联系方式" prop="contactInformation">
        <el-input v-model="pram.contactInformation" maxlength="100" placeholder="联系方式" />
      </el-form-item>
      <el-form-item label="备注：">
        <el-input v-model="pram.remark" type="textarea" :rows="5" placeholder="备注"/>
      </el-form-item>
      <el-form-item label="序号">
        <el-input-number v-model="pram.number" :min="0"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:protection:point:update','admin:protection:point:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as protectionPointApi from '@/api/protectionPoint.js'
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allTreeList: {
      type: Array
    },
    allCategoryData:{
      type: Object
    }
  },
  data() {
    /*const validatePhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请填写手机号'));
      } else if (!/^1[3456789]\d{9}$/.test(value)) {
        callback(new Error('手机号格式不正确!'));
      } else {
        callback();
      }
    };*/
    return {
      constants:this.$constants,
      pram: {
        name: null,
        longitude: null,
        latitude: null,
        radius:null,
        departmentId: null,
        contactInformation: null,
        remark:null,
        number:0,
        id: 0,
      },
      rules: {
        name: [{ required: true, message: '请填保护点名称', trigger: ['blur', 'change'] }],
        //type: [{ required: true, message: '请选类型', trigger: ['blur', 'change'] }],
        //area: [{ required: true, message: '请填分布区域', trigger: ['blur', 'change'] }],
        longitude: [{ required: true, message: '请填经度', trigger: ['blur', 'change'] }],
        latitude: [ { required: true, message: '请填纬度', trigger: ['blur', 'change']} ],
        radius: [{ required: true, message: '请填保护半径', trigger: ['blur', 'change'] }],
        departmentId: [{ required: true, message: '请选择所属单位', trigger: ['blur', 'change'] }],
        contactInformation: [ { required: true, message: '请填写联系方式', trigger: ['blur', 'change']} ],
      },
      categoryData:{
        typeList:[],
        personList:[]
      },
      parentOptions: [],
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'child',
        expandTrigger: 'hover',
        checkStrictly: true,
        emitPath: false
      },
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    initEditData() {
      this.categoryData = this.allCategoryData
      this.parentOptions = [...this.allTreeList]
      this.addTreeListLabelForCasCard(this.parentOptions, 'child')

      if (this.isCreate !== 1) return //编辑
      const { name, /*type, area, */radius, longitude, latitude, departmentId, contactInformation, remark, id,number } = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.name = name
      //this.pram.type = type
      //this.pram.area = area
      this.pram.radius = radius
      this.pram.longitude = longitude
      this.pram.latitude = latitude
      this.pram.departmentId = departmentId
      this.pram.contactInformation = contactInformation
      this.pram.remark = remark
      this.pram.id = id
      this.pram.number = number
    },
    addTreeListLabelForCasCard(arr, child) {
      arr.forEach((o,i) => {
        if (o.child && o.child.length) {
          o.disabled = true
          o.child.forEach((j) => {
            //j.disabled = true
          })
        }
      })
    },
    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      protectionPointApi.pointAdd(this.pram).then(data => {
        this.$message.success('创建保护点成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      protectionPointApi.pointAdd(this.pram).then(data => {
        this.$message.success('更新保护点成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped>

</style>
